package com.sgl.gk.service.impl;

import com.sgl.common.core.domain.AjaxResult;
import com.sgl.gk.controller.vo.GkStationVo;
import com.sgl.gk.domain.GkStationEvent;
import com.sgl.gk.domain.GkStationTypeEvent;
import com.sgl.gk.mapper.GkStationMapper;
import com.sgl.gk.mapper.GkStationTypeEventMapper;
import com.sgl.gk.service.GkStationTypeEventService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:xec
 * @date:2022/10/31 18:50
 **/
@Service
@Slf4j
public class GkStationTypeEventServiceImpl implements GkStationTypeEventService {

    @Autowired
    GkStationTypeEventMapper gkStationTypeEventMapper;

    @Autowired
    GkStationMapper gkStationMapper;
    @Override
    public AjaxResult list(GkStationTypeEvent gkStationTypeEvent) {
        List<GkStationTypeEvent> list = gkStationTypeEventMapper.list(gkStationTypeEvent);
        List<GkStationVo> gkStationVos = gkStationMapper.list(null);
        for (GkStationTypeEvent stationEvent : list) {

            String[] split = stationEvent.getConditionStationId().trim().split(",");
            ArrayList arrayList = new ArrayList();
            for (String s : split) {
                if (s.equals("")){
                    continue;
                }
                for (GkStationVo gkStationVo : gkStationVos) {
                    if (gkStationVo.getId().equals(Long.parseLong(s))) {
                        arrayList.add(gkStationVo.getName());
                    }
                }
            }
            stationEvent.setConditionStationNames(StringUtils.join(arrayList,","));
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult removeByIds(Long[] ids) {
        int row = gkStationTypeEventMapper.removeByIds(ids);
        if (row>0){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    @Override
    public AjaxResult add(GkStationTypeEvent gkStationTypeEvents) {
        int row = gkStationTypeEventMapper.add(Arrays.asList(gkStationTypeEvents));
        if (row>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult edit(GkStationTypeEvent gkStationTypeEvents) {
        int row = gkStationTypeEventMapper.edit(Arrays.asList(gkStationTypeEvents));
        if (row>0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


}
